﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using Mysqlx.Session;
using CKS.Core.Framework;
using Infrastructure.Enums;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 工位表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksWorkRecordService), ServiceLifetime = LifeTime.Transient)]
    public class CksWorkRecordService : BaseService<CksWorkRecord>, ICksWorkRecordService
    {
        /// <summary>
        /// 查询工位表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksWorkRecordDto> GetList(CksWorkRecordQueryDto parm) {
            var response = Queryable()
                .LeftJoin<PlcConfig>((recDt, plcDt) => recDt.PlcID == plcDt.Id)
                .LeftJoin<CksCollected>((recDt, plcDt, cltDt) => cltDt.WorkRecId == recDt.Id)
                .LeftJoin<CksPlcBlockDb>((recDt, plcDt, cltDt, dbDt) => recDt.PlcID == dbDt.PlcId && recDt.ReadDB == dbDt.BlockDb)
                .WhereIF(!(string.IsNullOrEmpty(parm.PrimaryCode)), (recDt, plcDt) => recDt.PrimaryCode == parm.PrimaryCode)
                .WhereIF(!string.IsNullOrEmpty(parm.LineCode), (recDt, plcDt) => plcDt.ProductionLineCode == parm.LineCode)
                .WhereIF(!string.IsNullOrEmpty(parm.StationCode), (recDt, plcDt) => plcDt.WorkstationCode == parm.StationCode)
                .WhereIF(parm.BeginTime.HasValue && parm.EndTime.HasValue, (recDt, plcDt) => recDt.CreatedTime >= parm.BeginTime.Value && recDt.CreatedTime <= parm.EndTime.Value)
                .Select((recDt, plcDt, cltDt, dbDt) => new CksWorkRecordDto {
                    Id = recDt.Id,
                    PlcID = recDt.PlcID,
                    ProductionLineCode = plcDt.ProductionLineCode,
                    StationCode = $"{plcDt.WorkstationCode}({plcDt.Remark})",
                    ReadDB = recDt.ReadDB,
                    DBBlockRemark = dbDt.Remark,
                    PrimaryCode = recDt.PrimaryCode,
                    MoveInTime = recDt.MoveInTime,
                    ProductionStartTime = recDt.ProductionStartTime,
                    ProductionEndTime = recDt.ProductionEndTime,
                    MoveOutTime = recDt.MoveOutTime,
                    CreatedTime = recDt.CreatedTime,
                    Result = cltDt.Result.HasValue ? cltDt.Result : null,
                    Nullity = cltDt.Nullity.HasValue ? (cltDt.Nullity.Value == false ? true : false) : null
                }).OrderByDescending(recDt => recDt.CreatedTime).ToPage(parm);
            return response;
        }

        /// <summary>
        /// 工件返工
        /// </summary>
        /// <param name="cksWorkRecords"></param>
        /// <returns></returns>
        public (bool isSuccess,string errorMsg) WorkStationReWork(List<CksWorkRecord> cksWorkRecords) {

            if (cksWorkRecords == null || cksWorkRecords.Count == 0) {
                return (false,"");
            }

            // 查询采集记录
            var cksCollectedList = Context.Queryable<CksCollected>().In(cltDt => cltDt.WorkRecId, cksWorkRecords.Select
                 (o => o.Id)).ToList();

            if (cksCollectedList != null && cksCollectedList.Count > 0) {

                // 更新返工状态
                foreach (var clt in cksCollectedList) {
                    if (clt.Nullity.HasValue && clt.Nullity.Value) {
                        return (false, "当前工作记录已为返工状态！");
                    }
                    clt.Nullity = true; // 设置为返工状态
                }

                // 更新采集记录
                return (Context.Updateable(cksCollectedList).ExecuteCommand() > 0, "");
            }

            return (false, "");
        }
    }
}